from selenium import webdriver
import time
import urllib.request


# 声明浏览器
browser = webdriver.Chrome()
browser.get('http://www.baidu.com')

# 将浏览器最大化
time.sleep(0.5)
browser.maximize_window()

time.sleep(0.5)
browser.find_element_by_id('kw').send_keys('深圳')
browser.find_element_by_id('su').click()

time.sleep(0.5)
browser.find_element_by_link_text('图片').click()

# 定位图片元素
Images = browser.find_elements_by_css_selector('div.imgbox img[class="main_img img-hover"]')

num = 0
for image in Images:
    url = image.get_attribute('data-imgurl')
    urllib.request.urlretrieve(url, str(num)+'img.jpg')
    num += 1
    if num == 5:
        break

time.sleep(0.5)
# 关闭浏览器
browser.quit()